#!/bin/bash
set -e

cleanup()
{
    local exit=$?
    kill $PID || true
    wait $PID || true
    exit $exit
}

cd $(dirname $0)/..

if grep -q overlay /proc/filesystems; then
    STORAGE=overlay
else
    STORAGE=aufs
fi
docker daemon -s $STORAGE -D >/var/log/docker.log 2>&1 &
PID=$!
trap cleanup EXIT

sleep 1

docker info

ip link set dev eth0 mtu 1300
ip link set dev docker0 mtu 1300

IDS=$(docker ps -qa)
if [ -n "$IDS" ]; then
    docker kill $(docker ps -qa) || true
    docker rm -fv || true
fi

echo Running tests

docker pull ibuildthecloud/helloworld:latest

docker run -i -d busybox:1

PACKAGES=". $(find -name '*.go' | xargs -I{} dirname {} |  cut -f2 -d/ | sort -u | grep -Ev '(^\.$|.git|.trash-cache|vendor|bin)' | sed -e 's!^!./!' -e 's!$!/...!')"

go test -race -cover -timeout=3m -tags=test ${PACKAGES}

cd tests
find -depth -name __pycache__ -o -name "*.pyc" -exec rm -rf {} \;
if [ -z "$NO_TEST" ]; then
    mkdir -p ${CATTLE_HOME:-/var/lib/cattle} || true
    tox "$@"
fi
